package com.syxx.oa.web.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.syxx.oa.domain.Department;
import com.syxx.oa.domain.EmpLevel;
import com.syxx.oa.service.EmpLevelService;
import com.syxx.oa.utils.StringUtils;
import com.test.jbpm.Pager;
import com.test.jbpm.web.action.BaseAction;

@Controller
@Scope("prototype")
public class EmpLevelAction extends BaseAction{

	private static final long serialVersionUID = -1259992718799904902L;
	private EmpLevelService empLevelService;
	private EmpLevel empLevel;
	private Long[] levIds;
	public EmpLevel getEmpLevel() {
		return empLevel;
	}
	public void setEmpLevel(EmpLevel empLevel) {
		this.empLevel = empLevel;
	}
	
	public EmpLevelService getEmpLevelService() {
		return empLevelService;
	}
	public Long[] getLevIds() {
		return levIds;
	}
	public void setLevIds(Long[] levIds) {
		this.levIds = levIds;
	}
	@Autowired
	public void setEmpLevelService(EmpLevelService empLevelService) {
		this.empLevelService = empLevelService;
	}
	
	public String addOrUpdate(){
		String retStr = "";
		try {
			empLevelService.addOrUpdate(empLevel);
			retStr = ajaxOut("{success: true}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retStr;
	}
	
	public String empLevel_index() throws Exception {
		return SUCCESS;
	}
	
	public String list() throws Exception {
		Pager<EmpLevel> pager = empLevelService.findList();
		StringBuffer sbJson = new StringBuffer();
		sbJson.append("{");
		sbJson.append("total:");
		sbJson.append(pager.getTotal());
		sbJson.append(", data:");
		List<EmpLevel> userList = pager.getData();
		sbJson.append(JSONArray.fromObject(userList));
		sbJson.append("}");
		//测试
		System.out.println("emplevel list json:"+sbJson.toString());
        return ajaxOut(sbJson.toString());
	}
	
	public String edit() throws Exception {
	    System.out.println("进入emplevel edit empLevel.getLevId()="+empLevel.getLevId());
	    EmpLevel point=new EmpLevel();
		empLevel = empLevelService.findById(empLevel.getLevId());
		point.setLevCode(empLevel.getLevCode());
		point.setLevId(empLevel.getLevId());
		point.setLevName(empLevel.getLevName());
		point.setAuthoritys(null);
		
		   System.out.println("进入emplevel edit 2");
		JSONObject jsonObject = JSONObject.fromObject(point);
		 System.out.println("进入emplevel edit json:"+jsonObject);
		return ajaxOut("{success:true, data:" + jsonObject + "}");
	}
	
	public String del() throws Exception{
		empLevelService.del(levIds);
		return ajaxOut();
	}
	
	public String findListby() throws Exception{
		List<EmpLevel> reList = empLevelService.findListBy(null);
		return ajaxOut(StringUtils.list2Json(reList).toString());
	}

}
